package org.springframework.security.oauth2.server.authorization.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.security.oauth2.server.authorization.client.WeChatMiniProgramService;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 微信小程序属性配置类
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
// @Data
@Component
@ConfigurationProperties("wechat.mini.program")
public class WeChatMiniProgramProperties {

	/**
	 * 微信小程序属性配置列表
	 */
	private List<WeChatMiniProgram> list;

	/**
	 * 默认微信小程序的权限
	 * <p>
	 * 若要自定义用户的权限，请开发者自己实现 {@link WeChatMiniProgramService}
	 */
	private String defaultRole;

	/**
	 * 默认 AppID
	 */
	// @Getter(AccessLevel.NONE)
	private String defaultAppid;

	public List<WeChatMiniProgram> getList() {
		return list;
	}

	public void setList(List<WeChatMiniProgram> list) {
		this.list = list;
	}

	public String getDefaultRole() {
		return defaultRole;
	}

	public void setDefaultRole(String defaultRole) {
		this.defaultRole = defaultRole;
	}

	public void setDefaultAppid(String defaultAppid) {
		this.defaultAppid = defaultAppid;
	}

	public String getDefaultAppid() {
		if (StringUtils.hasText(defaultAppid)) {
			return defaultAppid;
		}
		if (list == null) {
			return null;
		}
		if (list.size() > 0) {
			return list.get(0).appid;
		}
		return null;
	}

	/**
	 * 微信小程序属性配置类
	 *
	 * @author xuxiaowei
	 * @since 0.0.1
	 */
	// @Data
	public static class WeChatMiniProgram {

		/**
		 * AppID(小程序ID)
		 */
		private String appid;

		/**
		 * AppSecret(小程序密钥)
		 */
		private String secret;

		public String getAppid() {
			return appid;
		}

		public void setAppid(String appid) {
			this.appid = appid;
		}

		public String getSecret() {
			return secret;
		}

		public void setSecret(String secret) {
			this.secret = secret;
		}
	}

}
